package Lecture7;

public class Task2 {

	
	//Write a function to convert Roman Number in 
	//Decimal Format
	public static void main(String[] args)
	{

		String str = "MXII";
		convertRomanNum(str);
	}
	
	
	public static void convertRomanNum (String str)
	{
		int dec = 0;
		int num = 0;
		
		for(int i = str.length() - 1; i >= 0; i--)
		{
			
			char cr = str.charAt(i);
						
			switch (cr) 
			{
            case 'M':
                dec = calculateNum (1000, num, dec);
                num = 1000;
                break;

            case 'D':
                dec = calculateNum (500, num, dec);
                num = 500;
                break;

            case 'C':
                dec = calculateNum (100, num, dec);
                num = 100;
                break;

            case 'L':
                dec = calculateNum (50, num, dec);
                num = 50;
                break;

            case 'X':
                dec = calculateNum (10, num, dec);
                num = 10;
                break;

            case 'V':
                dec = calculateNum (5, num, dec);
                num = 5;
                break;

            case 'I':
                dec = calculateNum (1, num, dec);
                num = 1;
                break;
			}
		}
		System.out.println(dec);
	}
	
	
	public static int calculateNum (int dec, int num, int lastDec)  
	{
		if(num > dec)
		{
			return lastDec - dec;
		}
		else
		{
			return lastDec + dec;
		}
		
	}
	
	
	
	
	
}